/*
' Copyright (c) 2012 DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;

namespace DotNetNuke.Modules.TaskManager
{
   using DotNetNuke.Entities.Users;
   using DotNetNuke.Modules.TaskManager.Components;

   /// -----------------------------------------------------------------------------
   /// <summary>
   /// The EditTaskManager class is used to manage content
   /// 
   /// Typically your edit control would be used to create new content, or edit existing content within your module.
   /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
   /// 
   /// Because the control inherits from TaskManagerModuleBase you have access to any custom properties
   /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
   /// 
   /// </summary>
   /// -----------------------------------------------------------------------------
   public partial class Edit : TaskManagerModuleBase
   {

      #region Event Handlers

      override protected void OnInit(EventArgs e)
      {
         InitializeComponent();
         base.OnInit(e);
      }

      private void InitializeComponent()
      {
         this.Load += new System.EventHandler(this.Page_Load);
      }

      /// -----------------------------------------------------------------------------
      /// <summary>
      /// Page_Load runs when the control is loaded
      /// </summary>
      /// -----------------------------------------------------------------------------
      private void Page_Load(object sender, System.EventArgs e)
      {
         try
         {
            //Implement your edit logic for your module
            if (!Page.IsPostBack)
            {
               ddlAssignedUser.DataSource = UserController.GetUsers(PortalId);
               ddlAssignedUser.DataTextField = "Username";
               ddlAssignedUser.DataValueField = "UserId";
               ddlAssignedUser.DataBind();

               if (TaskId > 0)
               {
                  Task task = TaskController.GetTask(TaskId);
                  if (task != null)
                  {
                     txtName.Text = task.TaskName;
                     txtDescription.Text = task.TaskDescription;
                     txtTargetCompletionDate.Text = task.TargetCompletionDate.ToString();
                     txtCompletionDate.Text = task.CompletedOnDate.ToString();
                     ddlAssignedUser.Items.FindByValue(task.AssignedUserId.ToString()).Selected = true;
                  }
               }
            }
         }
         catch (Exception exc) //Module failed to load
         {
            Exceptions.ProcessModuleLoadException(this, exc);
         }
      }

      #endregion

      protected void btnSubmit_Click(object sender, EventArgs e)
      {
         Task task;
         if(TaskId>0)
            {
               task = TaskController.GetTask(TaskId);
               task.TaskName = txtName.Text.Trim();
               task.TaskDescription = txtDescription.Text.Trim();
               task.LastModifiedByUserId = UserId;
               task.LastModifiedOnDate = DateTime.Now;
               task.AssignedUserId = Convert.ToInt32(ddlAssignedUser.SelectedValue);
            }
            else
            {
               task = new Task
               {
                     AssignedUserId = Convert.ToInt32(ddlAssignedUser.SelectedValue),
                     //CompletedOnDate = Convert.ToDateTime(txtCompletionDate.Text.Trim()),
                     CreatedByUserId = UserId,
                     CreatedOnDate = DateTime.Now,
                     //TargetCompletionDate = Convert.ToDateTime(txtTargetCompletionDate.Text.Trim()),
                     TaskName = txtName.Text.Trim(),
                     TaskDescription = txtDescription.Text.Trim(),
                     ModuleId = ModuleId
               };
            }

         DateTime outputDate;
         if (DateTime.TryParse(txtCompletionDate.Text.Trim(), out outputDate))
         {
            task.CompletedOnDate = outputDate;
         }
         if (DateTime.TryParse(txtTargetCompletionDate.Text.Trim(), out outputDate))
         {
            task.TargetCompletionDate = outputDate;
         }

         TaskController.SaveTask(task, TabId);

         //If I wantedt to pass something back saying it was successful,
         //then I can change the parameters of the Navigate URL method.
         Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
      }

      protected void btnCancel_Click(object sender, EventArgs e)
      {
         //Navigate URL with no parameters generates a URL for the current page, with no query parameters.
         Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
      }

   }

}